import { View } from '@tarojs/components';
import './index.scss';
import { useEffect, useState } from 'react';
import Taro from '@tarojs/taro';
import { historyInfo } from '../../configProvider/backendInterface';
import HistoryCard from '../../components/Card/HistoryCard';
import { IP_NAME } from '../../configProvider/namespace';
const SelectHistory = () => {
  const [historyList, setHistoryList] = useState<historyInfo[]>([]);
  useEffect(() => {
    Taro.request({
      url: `${IP_NAME}/conversation/get_group_msg?counselor_name=瓜皮&user_name=林忆宁&page=1&size=10&order=id asc`, //仅为示例，并非真实的接口地址
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        console.log(res.data.data.items)
        setHistoryList(res.data.data.items);
      }
    });
  }, [])
  const trySendHistory = (index: number) => {
    Taro.showModal({
      title: '提示',
      content: '选择发送这条记录？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          Taro.setStorage({
            key: 'historyToSend',
            data: historyList[index]
          })
          Taro.reLaunch({ url: '../TUIChat/index' })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
  return <View>
    <View className='common-title'>历史记录</View>
    <View className='history-record'>
      {historyList.map((value, index) => {
        return <HistoryCard history={value} onClick={() => trySendHistory(index)} />
      })}</View>
  </View>
}
export default SelectHistory;
